package com.hb.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.order.dao.StudentMapper;
import com.hb.order.model.Student;
import com.hb.order.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = {"swagger 模块学习"}, value = "数据字典控制器")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentMapper studentMapper;

    @ApiOperation("mybatis 测试")
    @GetMapping("/getUser/{id}")
    public String GetUser(@PathVariable int id) {
        Student student = studentService.sell(id);
        return student.toString();
    }

    // 使用 @Transactional 注解，则表示是事务
    @Transactional
    @GetMapping("/addUsers")
    public String addUsers() {
        Student student1 = new Student("password", "张三", "zs", 18, 1, "111");
        Student student2 = new Student("pwd", "李四", "lisi", 18, 0, "222");
        ArrayList<Student> studentsList = new ArrayList<>();
        studentsList.add(student1);
        studentsList.add(student2);
        // 执行添加数据的动作
        List<BatchResult> insert = studentMapper.insert(studentsList);
        // 放开下面的代码，则抛出异常，查询数据库，没有插入成功
        //Integer i = 10 / 0;
        return insert.toString();
    }

    @GetMapping("/delUsers")
    public String delUsers() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("age", 18);
        List list = studentMapper.selectList(queryWrapper);
        int i = studentMapper.deleteBatchIds(list);
        return i+"";
    }
}